package com.wlb365.financing.modules.template.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlb365.financing.common.dto.R;
import com.wlb365.financing.common.exception.AdminException;
import com.wlb365.financing.common.utils.ShiroUtil;
import com.wlb365.financing.modules.sys.entity.Dict;
import com.wlb365.financing.modules.sys.service.IDictService;
import com.wlb365.financing.modules.template.request.SubscribeDataReq;
import com.wlb365.financing.modules.template.request.TemplateQueryByTypeReq;
import com.wlb365.financing.modules.template.request.TemplateQueryPageReq;
import com.wlb365.financing.modules.template.request.TemplateReq;
import com.wlb365.financing.modules.template.response.TemplatePageRes;
import com.wlb365.financing.modules.template.response.TemplateRes;
import com.wlb365.financing.modules.template.service.ITemplateService;
import com.wlb365.sdk.client.SdkClient;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.velocity.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JsonParseException;
import org.springframework.boot.json.JsonParser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


/**
 * 模板管理
 * @Author LuHang
 * @Date 2021/5/18 9:59
 * @Version 1.0
 */
@RestController
@RequestMapping("template")
public class TemplateController {

    @Autowired
    private SdkClient sdkClient;

    @Autowired
    private ITemplateService templateService;

    @Autowired
    private IDictService dictService;


    @RequiresPermissions("template:index")
    @GetMapping(value = "index")
    public ModelAndView templateIndex(){
        ModelAndView modelAndView = new ModelAndView("template/template");
        return modelAndView;
    }

    @GetMapping(value = "list/dictData")
    public R listData(Dict dict) {
        Dict dictResult = dictService.getByCode(dict.getCode());
        Page<Dict> page = null;
        if(dictResult!=null){
            Dict dict1 = new Dict();
            dict1.setId(dictResult.getId());
            page = dictService.listDictPage(dict1);
        }
        return R.ok(page);
    }

    /**
     * 新增跳转
     * @return
     */
    @RequiresPermissions("template:add")
    @GetMapping(value = "add")
    public ModelAndView addView(){
        ModelAndView modelAndView = new ModelAndView("template/template_edit");
        Dict dictResult = dictService.getByCode("template");
        Dict dict = new Dict();
        dict.setId(dictResult.getId());
        dict.setCurrent(1);
        dict.setType(1);
        dict.setSize(999);
        Page<Dict> page = dictService.listDictPage(dict);
        modelAndView.addObject("dictList",page.getRecords());
        // 1 是新增 2 是修改
        modelAndView.addObject("type",1);
        return modelAndView;
    }


    /**
     * 编辑回显
     * @param id
     * @return
     */
    @RequiresPermissions("template:edit")
    @GetMapping(value = "edit")
    public ModelAndView editView(String id){
        ModelAndView modelAndView = new ModelAndView("template/template_edit");
        TemplateRes templateRes = templateService.selectOne(id);
        Dict dict = dictService.getByCode(templateRes.getTemplateBtype());
        // 1 是新增 2 是修改
        modelAndView.addObject("type",2);
        modelAndView.addObject("template",templateRes);
        modelAndView.addObject("codeName",dict!=null?dict.getName():"暂无该数据");
        return modelAndView;
    }

    /**
     * 修改模板信息
     * @param templateReq
     * @return
     */
    @PostMapping(value = "editTemplate")
    public R editTemplate(final TemplateReq templateReq){
        TemplateRes templateRes = templateService.update(templateReq);
        if(null == templateRes){
           throw new AdminException("修改模板失败" + templateRes.getMsg() + ": " + templateRes.getSubMsg());
        }
        return R.ok();
    }

    /**
     * 删除模板信息
     * @param ids
     * @return
     */
    @RequiresPermissions("template:del")
    @PostMapping(value = "del")
    public R del(@RequestParam("ids")  List<String> ids){
        boolean isDelete = templateService.del(ids);
        return R.ok();
    }

    /**
     * 验证是否创建该模板
     * @param templateQueryByTypeReq
     * @return
     */
    @GetMapping(value = "selectOneByType")
    public TemplateRes selectOneByType(final TemplateQueryByTypeReq templateQueryByTypeReq){
        TemplateRes templateRes = templateService.selectOneByType(templateQueryByTypeReq);
        return templateRes;
    }

    /**
     * 新增模板
     * @return
     */
    @PostMapping(value = "addTemplate")
    public R addTemplate(final TemplateReq templateReq){
        try {
            JSONObject jsonObject = JSONObject.parseObject(templateReq.getTemplateData());
        } catch (Exception e) {
            throw new AdminException("JSON格式异常");
        }
        templateReq.setTemplateType(1);
        TemplateRes templateRes = templateService.addTemplate(templateReq);
        if(null!=templateRes){
            SubscribeDataReq subscribeDataReq = new SubscribeDataReq();
            subscribeDataReq.setTemplateId(templateRes.getId());
            subscribeDataReq.setSubscribeBy(ShiroUtil.getUser().getDeptId());
            templateService.addSubscribe(subscribeDataReq);
        }else{
            throw new AdminException("新增模板失败" + templateRes.getMsg() + ": " + templateRes.getSubMsg());
        }
        return R.ok();
    }

    /**
     * 分页查询模板数据
     * @param templateQueryPageReq
     * @return
     */
    @GetMapping(value = "/listData")
    public TemplatePageRes listData(final TemplateQueryPageReq templateQueryPageReq){
        TemplatePageRes templatePageRes = templateService.queryTemplatePage(templateQueryPageReq);
        return templatePageRes;
    }

}
